package top.noteday.impl;

import java.util.List;
import java.util.Date;
import entity.Page;
import entity.form.FormDto;
import entity.form.FormVo;
import entity.result.EntityResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import utils.common.StrUtil;
import entity.field.*;
import top.noteday.dao.field.FieldDao;
import top.noteday.provider.field.FieldService;

@Component
public class FieldServiceImpl implements FieldService {

	private Logger LOGGER = LoggerFactory.getLogger(FieldServiceImpl.class);

	@Autowired
	private FieldDao fieldDao;

    /*
    @Override
    PageResult<FieldDto> selectField(FieldVo fieldVo, Page page) {
        FieldQuery fieldQuery = new FieldQuery();
        PageResult<FieldDto> result = new PageResult<>();

        // TODO 设置查询信息
        fieldQuery.setCreator(userId);
        fieldQuery.setPage(page);

        // 设置查询条数
        int count = fieldDao.selectFieldCount(fieldQuery);
        result.setTotalCount(count);

        List<FieldDto> list = new ArrayList<>();
        if (count > 0) {
            list = fieldDao.selectField(fieldQuery);

            // 返回值信息缺少时，赋值默认值
            for(CommissionDto dto : list) {
                // TODO 设置返回值默认信息
                //if(StrUtil.isEmpty(dto.getBorrowerPhone())) { dto.setBorrowerPhone(""); }
                //if(StrUtil.isEmpty(dto.getBuildArea())) { dto.setBuildArea(new BigDecimal("0.00")); }
            }
        }

        result.setData(list);
        result.setCode(ResponseCodeEnum.SUCCESS.getCode());
        result.setMess(ResponseCodeEnum.SUCCESS.getMessage());
        result.setSuccess(true);
        return result;
    }
    */

    /**
     * 获取数据
     **/
    @Override
    public Field selectById(Integer id) {
        FieldQuery query = new FieldQuery();
        query.setUserId(id);

        List<FieldDto> list = fieldDao.selectByParam(query);

        return StrUtil.isEmpty(list) ? null : list.get(0);
    }

    /**
     * 获取数据
     **/
    @Override
    public int clearFieldByFormId(Integer id) {
        FieldQuery query = new FieldQuery();
        query.setFormId(id);
        return fieldDao.clearFieldByFormId(query);
    }

    /**
     * 保存属性模板
     */
    @Override
    @Transactional
    public EntityResult<FieldDto> saveField(FieldVo vo) {
        LOGGER.info("保存属性模板{} = ", vo);
        if (StrUtil.isEmpty(vo.getId())) {
            return this.insertField(vo);
        }
        return this.updateField(vo);
    }


    private EntityResult<FieldDto> insertField(FieldVo vo) {
        EntityResult<FieldDto> result = new EntityResult();
        // 保存表单模板
        vo.setCreator(vo.getUserId());
        vo.setCreateTime(vo.getDate());
        vo.setOperator(vo.getUserId());
        vo.setUpdateTime(vo.getDate());
        int count = fieldDao.insert(vo);
        StrUtil.paramIsCorrect(count != 0, "属性保存错误");

        return result;
    }

    private EntityResult<FieldDto> updateField(FieldVo vo) {
        EntityResult<FieldDto> result = new EntityResult();
        // 保存表单模板
        vo.setOperator(vo.getUserId());
        vo.setUpdateTime(vo.getDate());
        int count = fieldDao.updateById(vo);
        StrUtil.paramIsCorrect(count != 0, "表单模板保存错误");

        return result;
    }
}
